<template>
    <van-icon :color="(value === 1) ? '#e5645f' : '#777'" :name="(value === 1) ? 'good-job' : 'good-job-o'"
        @click="OnLike" />
</template>

<script>
import { addLike, deleteLike } from '../api/article'

export default {
    name: 'LikeArticle',
    props: {
        value: {
            type: [Number, String],
            required: true
        },
        art_id: {
            type: [String, Number, Object],
            required: true
        }
    },
    methods: {
        async OnLike() {
            try {
                let likeStatus = -1
                if (this.value === 1) {
                    await deleteLike(this.art_id)
                } else {
                    await addLike(this.art_id)
                    likeStatus = 1
                }
                this.$emit('input', likeStatus)
                this.$nextTick(() => {
                    this.$toast.success((likeStatus === 1) ? '点赞成功' : '取消点赞')
                })
            } catch (err) {
                console.log('点赞失败', err);
                this.$toast.fail('操作失败，请稍后重试')
            }
        }
    }
}
</script>

<style>
</style>